import os,re
from conan import ConanFile
from conan.tools.files import copy
from conan.tools.files import rename
from conan.tools.scm import Git
from os.path import join

class CommonSourceCppConanConan(ConanFile):
    name = "common_source_cpp"
    version = "master"
    description = "C/C++/C++11公用代码"
    url = "https://gitee.com/l0km/common_source_cpp"
    license = "BSD-2-Clause"
    author = "guyadong 10km0811@sohu.com"
    topics = ("C", "C++", "C++11", "common")

    settings = "compiler"

    def export(self):
        # copy(self,"conanfile_master.py",src=self.recipe_folder,dst=self.export_folder)
        # rename(self,join(self.export_folder,"conanfile_master.py"),join(self.export_folder,"conanfile.py"))
        copy(self,"conanfile.py",src="conanfile_master.py",dst=self.export_folder)
        
    def source(self):
        # 从远端仓库下载指定分支代码
        git = Git(self)
        clone_args = ['--branch', self.version]
        git.clone(url=self.url + ".git", args=clone_args, target=".")

    def package(self):
        # 确保当前仓库与远程仓库同步
        git = Git(self)
        self.output.info(git.run("pull"))
        copy(self,"*.h", self.source_folder, join(self.package_folder, "include"))
        copy(self,"*.cpp", self.source_folder, join(self.package_folder, "include"))
        copy(self,"*.hpp", self.source_folder, join(self.package_folder, "include"))
        copy(self,"*.hh", self.source_folder, join(self.package_folder, "include"))
        copy(self,"*.c", self.source_folder, join(self.package_folder, "include"))
        copy(self,"LICENSE", self.source_folder, join(self.package_folder, "include"))

    def package_id(self):
        self.info.clear()

    def package_info(self):
        if self.settings.compiler == "msvc":
            self.cpp_info.includedirs.append("include/dirent")

    def is_true(self,str):
        return re.match(r'^(?:true|on|1|y(es))?$',str,re.I)

    def configure(self):
		# 指定 common_source_cpp为纯C项目
        if self.is_true(os.getenv("COMMON_SOURCE_CPP_ONLY_C","False")) :
            del self.settings.compiler.libcxx
            del self.settings.compiler.cppstd